<?php
/*********************/
/*                   */
/*  Version : 5.1.0  */
/*  Author  : RM     */
/*  Comment : 071223 */
/*                   */
/*********************/

class model_admin_admin extends model {
	
	public function __construct() {
		parent::__construct();
		$this->_table = $this->db->options['prefix'] . "admin";
		$this->_primary = "userid";
		$this->_fields = array("userid", "roleid", "departmentid", "name", "sex", "birthday", "photo", "qq", "msn", "telephone", "mobile", "address", "zipcode", "created", "createdby", "updated", "updatedby", "disabled", "pv", "uv", "ip", "posts", "comments" );
		$this->_readonly = array("created", "createdby" );
		$this->_create_autofill = array("created" => TIME, "createdby" => $this->_userid, "disabled" => 0 );
		$this->_update_autofill = array("updated" => TIME, "updatedby" => $this->_userid );
		$this->_filters_input = array();
		$this->_filters_output = array();
		$this->_validators = array();
	}
	
	public function setpriv($userid, $catid, $sectionid) {
		$catid = array_filter( array_map( "intval",( array ) $catid ) );
		$catpriv = $this->db->select( "SELECT * FROM #table_category_priv WHERE userid=" . $userid );
		$origcatid = array();
		foreach( $catpriv as $c ) {
			$origcatid[] = $c['catid'];
		}
		$todel = array_diff( $origcatid, $catid );
		if($todel) {
			$where = "userid=" . $userid . " AND catid IN( " . implode_ids( $todel ) . " )";
			$this->db->exec( "DELETE FROM #table_category_priv WHERE " . $where );
		}
		$toadd = array_diff( $catid, $origcatid );
		foreach( $toadd as $r ) {
			$this->db->insert( "INSERT INTO #table_category_priv(catid,userid) VALUES(?, ?)", array($r, $userid ) );
		}
		$sectionid = array_filter( $sectionid );
		$sectionpriv = $this->db->select( "SELECT * FROM #table_section_priv WHERE userid=" . $userid );
		$origsectionid = array();
		foreach( $sectionpriv as $c ) {
			$origsectionid[] = $c['sectionid'];
		}
		$todel = array_diff( $origsectionid, $sectionid );
		if($todel) {
			$where = "userid=" . $userid . " AND sectionid IN( " . implode_ids( $todel ) . " )";
			$this->db->exec( "DELETE FROM #table_section_priv WHERE " . $where );
		}
		$toadd = array_diff( $sectionid, $origsectionid );
		foreach( $toadd as $r ) {
			$this->db->insert( "INSERT INTO #table_section_priv(sectionid,userid) VALUES(?, ?)", array($r, $userid ) );
		}
		priv::cache( $userid, table( "admin", $userid, "roleid" ) );
	}
	
	public function login($userid) {
		$a = $this->get( $userid );
		if(!$a) {
			$this->error = "{$_POST['username']} 不是管理员";
			return FALSE;
		}
		if($a['disabled']) {
			$this->error = "{$_POST['username']} 已被禁用";
			return FALSE;
		}
		return $a;
	}
	
	public function logout() {
	}
	
	public function _after_insert($data) {
		$this->cache();
	}
	
	public function _after_update($data) {
		$this->cache();
	}
	
	public function _after_delete($where) {
		$this->cache();
	}
	
	private function cache() {
		$db_cache = & factory::db_cache();
		$db_cache->set( "admin" );
	}

}

?>
